package com.code.test;

import com.alibaba.fastjson.JSON;
import com.code.bean.User;
import com.code.config.PropertyValuesConfig;
import org.junit.Test;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.core.env.ConfigurableEnvironment;

/**
 *
 * @author wangjie
 * @version V1.0
 * @date 2020/1/6
 */
public class IOCTest_PropertyValues {

    AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext(PropertyValuesConfig.class);


    @Test
    public void test01(){
        printBeans(applicationContext);
        System.out.println("=============");

        User user = (User) applicationContext.getBean("user");
        System.out.println(JSON.toJSONString(user));


        ConfigurableEnvironment environment = applicationContext.getEnvironment();
        String property = environment.getProperty("user.name");
        System.out.println(property);
        applicationContext.close();
    }

    private void printBeans(AnnotationConfigApplicationContext applicationContext){
        String[] definitionNames = applicationContext.getBeanDefinitionNames();
        for (String name : definitionNames) {
            System.out.println(name);
        }
    }
}
